package com.hyk.commonLib.common.drawable;

import android.graphics.Bitmap;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import androidx.annotation.IntRange;
import androidx.annotation.Nullable;

/**
 * Created by 贺玉琨 on 2019/3/7 0012.
 */

public abstract class BaseDrawable extends Drawable {
    protected Paint paint;

    protected BaseDrawable() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setDither(true);
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    protected Matrix getCenterMatrix(double sizeRate, Bitmap bitmap) {
        float width = getBounds().width();
        float height = getBounds().height();
        int drawableWidth = (int) (width * sizeRate);
        int drawableHeight = (int) (height * sizeRate);
        float scaleX = drawableWidth / (float) bitmap.getWidth();
        float scaleY = drawableHeight / (float) bitmap.getHeight();
        float scaleRate = Math.min(scaleX, scaleY);
        float positionX = (width - bitmap.getWidth() * scaleRate) / 2;
        float positionY = (height - bitmap.getHeight() * scaleRate) / 2;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleRate, scaleRate);
        matrix.postTranslate(positionX, positionY);
        return matrix;
    }
}
